<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/imgs/logo.jpg" alt="" />
      </div>
      <div class="title">狼人杀网页版</div>
      <!-- 登录表单区域 -->

      <el-form
        :model="loginForm"
        ref="loginFormRef"
        class="login_form"
        label-width="0px"
        :rules="loginFormRules"
      >
        <el-form-item label="" prop="username">
          <el-input v-model="loginForm.username" :prefix-icon="User"></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            v-model="loginForm.password"
            :prefix-icon="Lock"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="" class="btns">
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { login } from '../api/api.js'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

const store = useStore()

const router = useRouter()

const loginFormRef = ref(null)

const loginForm = reactive({
  username: 'root',
  password: 'root',
})

const loginFormRules = reactive({
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur',
    },
    {
      max: 10,
      min: 3,
      message: '用户名长度必须在3-10个字符之间',
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
  ],
})

const handleLogin = async () => {
  loginFormRef.value.validate(async (valid) => {
    if (!valid) {
      return
    }
    const { data: response } = await login(loginForm)

    if (response.code != 200) {
      ElMessage.error('登录失败')
      return
    }

    store.commit('updateUserInfo', response.data)

    sessionStorage.setItem('userinfo', JSON.stringify(response.data))
    ElMessage({
      message: '登录成功',
      type: 'success',
    })
    router.push('/home')
  })
}

const resetLoginForm = () => {
  loginFormRef.value.resetFields()
}
</script>

<style scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  position: relative;
}

.login_box {
  position: absolute;
  width: 450px;
  height: 300px;
  background-color: #04172c;
  opacity: 0.8;
  border-radius: 3px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_box .avatar_box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
}

.login_box .avatar_box img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.title {
  position: absolute;
  color: #1d3759;
  top: 30%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 20px;
}
</style>
